package br.com.jersey.client;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;

public class HelloClient {

	static ClientConfig config = null;
	static Client client = null;
	static WebResource service = null;
	
	public static void main(String[] args) throws JSONException {

		config = new DefaultClientConfig();
		client = Client.create(config);
		service = client.resource(getBaseURI());
		client.addFilter(new LoggingFilter());
		testMetodoGet();
		testMetodoPost();
		
	}
	
	public static void testMetodoGet(){
		System.out.println(service.path("get").accept(MediaType.TEXT_PLAIN)
				.get(String.class).toString());
	}
	
	public static void testMetodoPost() throws JSONException{
		JSONObject inputJsonObj = new JSONObject();
		inputJsonObj.put("input", "Value");
		System.out.println(service.path("post")
				.accept(MediaType.APPLICATION_JSON)
				.post(JSONObject.class, inputJsonObj));
	}

	private static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost:8088/jersey/rest/carro")
				.build();
	}

}
